﻿using CaptchaGen;
using LuShanTea.Common;
using LuShanTea.DTO;
using LuShanTea.Front.Web.App_Start;
using LuShanTea.Front.Web.Models;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Front.Web.Controllers
{
    public class RegisterController : Controller
    {
        ///发送短信填写的验证码
        public const string IMGCODE = "IMGCODE";
        //记录注册时邮箱
        public const string EMAIL = "EMAIL";
        //记录给邮箱发送的验证码
        public const string EMAILCODE = "EMAILCODE";

        public IUserService UserService { get; set; }

        public ICartService CartService { get; set; }

        // GET: Register
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(RegisterInfoModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", Data = MVCHelper.GetValidMsg(ModelState) });
            }
            string emailCode = (string)TempData[EMAILCODE];
            if (emailCode != model.ValidCode)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "验证码错误" });
            }
            var en = UserService.GetByEmailOrPhoneNum(model.Email, model.PhoneNum);
            if (en != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "电话号码或邮箱已被注册" });
            }

            if (TempData[EMAIL].ToString() != model.Email)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "邮箱错误" });
            }

            UserAddNewDTO dto = new UserAddNewDTO();
            dto.NickName = model.NickName;
            dto.Email = model.Email;
            dto.Gender = model.Gender;
            dto.PhoneNum = model.PhoneNum;
            dto.Pwd = model.PassWord;
            long userId = UserService.AddNew(dto);
            CartService.AddNew(userId);
            return Json(new AjaxResult { Status = "ok" });
        }


        public ActionResult GetValidCode()
        {
            string res;
            string code = CommonHelper.CreateVerifyCode(out res);
            TempData[IMGCODE] = res;
            Stream ms = ImageFactory.GenerateImage(code, 40, 120, 12, 2);
            return File(ms, "image/jpeg");
        }

        /// <summary>
        /// 短信，汝鹏网短信平台。账户：lck,密码：123
        /// 邮箱验证：
        /// </summary>
        /// <returns></returns>
        public ActionResult SendValidCode(RegisterSendValidCodeModel model)
        {

            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }
            //读取发送短信时填写的验证码
            string code = (string)TempData[IMGCODE];
            if (code != model.Code)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "验证码错误" });
            }
            if (UserService.GetByEmail(model.Email) != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "邮箱已存在" });
            }
            //保存用户的邮箱
            TempData[EMAIL] = model.Email;

            //创建一个发送给手机的验证码
            string validCode = CommonHelper.GetPhoneValidCode();
            //保存发给用户的验证码
            TempData[EMAILCODE] = validCode;

            LuShanEmailSms sms = new LuShanEmailSms();
            sms.Code = validCode;
            sms.Email = model.Email;

            ///发送验证码
            return Json(sms.Send());
        }


    }
}